<template>
  <div class="axiom-error-page">
    <AxiomThemeSwitch class="theme-switcher" />

    <AxiomFeatureCard
      class="login-card"
      icon="fas fa-satellite-dish"
      title="Axiom-View 登录"
      description="请输入您的凭据以访问控制面板"
    >
      <template #content>
        <form @submit.prevent="handleLogin" class="login-form">
          <div class="form-item">
            <AxiomInput
              v-model="loginForm.username"
              placeholder="请输入用户名"
              size="large"
              prefix-icon="fas fa-user"
            />
          </div>
          <div class="form-item">
            <AxiomInput
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
              size="large"
              prefix-icon="fas fa-lock"
            />
          </div>
          <AxiomButton
            type="primary"
            native-type="submit"
            :loading="loading"
            long
            size="large"
          >
            {{ loading ? '登录中...' : '登 录' }}
          </AxiomButton>
        </form>
      </template>
    </AxiomFeatureCard>
  </div>
</template>

<script setup>
import { reactive, ref, inject } from 'vue';
import { useRouter } from 'vue-router';
import { AxiomInput, AxiomButton, AxiomFeatureCard, AxiomThemeSwitch } from 'axiom-view';
// [重构] 导入应用自身的登录逻辑
import { loginByPassword } from '@/api/auth'; 

const axiomUtils = inject('axiom-utils');
const router = useRouter();

const loginForm = reactive({
  username: 'admin',
  password: 'axiom-admin',
});
const loading = ref(false);

const handleLogin = async () => {
  if (loading.value) return;
  loading.value = true;
  
  try {
    // [重构] 调用应用自身的登录函数，并传入所需的 auth 配置
    await loginByPassword(loginForm.username, loginForm.password, axiomUtils.auth);
    
    // 登录成功后跳转
    const redirect = router.currentRoute.value.query.redirect;
    router.push(typeof redirect === 'string' ? redirect : '/');
  } catch (error) {
    // 错误通知已由 apiClient 全局处理，此处仅记录日志
    console.error('登录流程中止:', error.message);
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.theme-switcher {
  position: absolute;
  top: 1.5rem;
  right: 1.5rem;
  z-index: 10;
}

.login-card {
  width: 100%;
  max-width: 420px;
}

.login-form {
  margin-top: 1.5rem;
}

.form-item {
  margin-bottom: 1.5rem;
}
</style> 